﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;

namespace WiFiChannelSpread
{
    public static class AnimationHelper
    {
        private const int DefaultDurationMilliseconds = 250;

        public static void FadeIn(FrameworkElement targetObject, double toValue)
        {
            targetObject.Opacity = 0;
            if (targetObject.Visibility != Visibility.Visible)
            {
                targetObject.Visibility = Visibility.Visible;
            }

            Fade(targetObject, toValue);
        }

        public static void Fade(FrameworkElement targetObject, double toValue)
        {
            AnimationHelper.StartDoubleAnimation(targetObject, FrameworkElement.OpacityProperty, toValue);
        }

        public static void StartDoubleAnimation(
            FrameworkElement targetObject, DependencyProperty targetProperty,
            double toValue)
        {
            StartDoubleAnimation(targetObject, targetProperty, toValue, null, DefaultDurationMilliseconds);
        }

        public static void StartDoubleAnimation(
            FrameworkElement targetObject, DependencyProperty targetProperty,
            double toValue,
            Action completedHandler)
        {
            StartDoubleAnimation(targetObject, targetProperty, toValue, completedHandler, DefaultDurationMilliseconds);
        }

        public static void StartDoubleAnimation(
            FrameworkElement targetObject, DependencyProperty targetProperty,
            double toValue,
            int durationMilliseconds)
        {
            StartDoubleAnimation(targetObject, targetProperty, toValue, null, durationMilliseconds);
        }

        public static void StartDoubleAnimation(
            FrameworkElement targetObject, DependencyProperty targetProperty,
            double toValue,
            Action completedHandler,
            int durationMilliseconds)
        {
            DoubleAnimation a = new DoubleAnimation(toValue, new Duration(TimeSpan.FromMilliseconds(durationMilliseconds)));
            Storyboard sb = new Storyboard();
            Storyboard.SetTargetProperty(a, new PropertyPath(targetProperty));
            sb.Children.Add(a);
            if (completedHandler != null)
            {
                sb.Completed += (s, ea) => { completedHandler(); };
            }

            sb.Begin(targetObject);
        }
    }
}
